#!/usr/bin/env bash
# Summary: Show the current Java version
set -e
[ -n "$JENV_DEBUG" ] && set -x

if [ -z "$JENV_VERSION" ]; then
  JENV_VERSION_FILE="$(jenv-version-file)"
  JENV_VERSION="$(jenv-version-file-read "$JENV_VERSION_FILE" || true)"
fi

if [ -z "$JENV_VERSION" ] || [ "$JENV_VERSION" = "system" ]; then
  echo "system"
  exit
fi

version_exists() {
  local version="$1"
  [ -d "${JENV_ROOT}/versions/${version}" ]
}

if version_exists "$JENV_VERSION"; then
  echo "$JENV_VERSION"
elif version_exists "${JENV_VERSION#java-}"; then
  { echo "warning: ignoring extraneous \`java-' prefix in version \`${JENV_VERSION}'"
    echo "         (set by $(jenv-version-origin))"
  } >&2
  echo "${JENV_VERSION#java-}"
else
  echo "jenv: version \`$JENV_VERSION' is not installed" >&2
  exit 1
fi
